package com.evola.rm.utils
{
	import com.evola.rm.model.ResourceKey;
	import com.evola.rm.view.project.ProjectModel;
	import com.evola.rm.view.project.tree.ResourceKeyTreeItem;
	import com.evola.rm.model.ResourceItem;

	public class FilterUtils
	{

		public static function treeFilterFunction(node:ResourceKeyTreeItem, searchString:String):Boolean
		{
			searchString=searchString.toLowerCase();

			// begin filtering
			var allowed:Boolean=false;

			// match searchString
			if (searchString == "")
			{
				allowed=true;
			}
			else if (node.expression.toLowerCase().indexOf(searchString) != -1)
			{
				allowed=true;
			}
//			else
//			{
//
//				for each (var child:ResourceKeyTreeItem in node.children)
//				{
//
//					if (filterFunction(child, searchString))
//					{
//
//						allowed=true;
//						break;
//					}
//				}
//			}

			return allowed;
		}

		public static function listFilterFunction(node:ResourceKey, model:ProjectModel):Boolean
		{
			var searchString:String=model.searchText;

			searchString=searchString.toLowerCase();

			// begin filtering
			var allowed:Boolean=false;

			// match searchString
			if (!searchString)
			{
				allowed=true;
			}
			else if (node.expression.toLowerCase().indexOf(searchString) != -1)
			{
				allowed=true;
			}

			if (allowed && model.filterMissingKeys)
			{

				var item:ResourceItem=model.selectedLanguage.getItem(node.expression);

				if (item && item.value != model.project.missingValue)
					allowed=false;
			}
			
			if(allowed && model.searchAlias){
				
				allowed = node.expression.toLowerCase().indexOf(model.searchAlias.code) == 0;
			}

			return allowed;
		}

	}
}
